﻿using System;
using System.Collections.Generic;
using Domino;

namespace T2ReportsAssistant
{
    public static class NotesUtilities
    {
        /// <summary>
        /// Get Public Address Book for the open session
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static NotesDatabase GetPublicAddressBook(NotesSession session)
        {
            if (session.AddressBooks != null)
            {
                Array aBooks = (Array)session.AddressBooks;
                NotesDatabase book;
                foreach (object db in aBooks)
                {
                    book = db as NotesDatabase;
                    if ((book != null) && (book.IsPublicAddressBook))
                    {
                        return book;
                    }
                }
            }

            return null;
        }

        /// <summary>Find contacts in address book</summary>
        /// <param name="addressBook">Address Book</param>
        /// <param name="fieldName">Full name of the field on which to search. Register does not matter.</param>
        /// <param name="searchPattern">Template (pattern) string to search. You can use the symbols C _%. Register does not matter.</param>
        /// <param name="maxCount">Limit on the number of contacts returned by the function. Value "0" removes the restriction.</param>
        /// <returns>Returns a list of found contacts</returns>
        public static AddressBookContact[] SearchAddressBookContact(NotesDatabase addressBook, string fieldName, string searchPattern, int maxCount)
        {
            if (!addressBook.IsOpen) addressBook.Open();

            string formula = String.Format("SELECT (Type = 'Person' & @Like(@LowerCase({0}); @LowerCase('{1}')))", fieldName, searchPattern);
            NotesDocumentCollection documents = addressBook.Search(formula, null, maxCount);

            List<AddressBookContact> contacts = new List<AddressBookContact>(documents.Count);

            for (int docCount = 1; docCount <= documents.Count; docCount++)
            {
                contacts.Add(new AddressBookContact(documents.GetNthDocument(docCount)));
            }

            return contacts.ToArray();
        }

        /// <summary>
        /// The list of file names, embedded in the document.
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        public static String[] GetAttachmentNames(NotesDocument document)
        {
            List<string> attachmentNames = new List<string>();

            if (document.HasEmbedded)
            {
                Array evalArray = document.ParentDatabase.Parent.Evaluate("@AttachmentNames", document) as Array;

                if (evalArray != null)
                {
                    foreach (object sObj in evalArray)
                    {
                        string fileName = sObj as string;
                        if (!String.IsNullOrEmpty(fileName))
                        {
                            attachmentNames.Add(fileName);
                        }
                    }
                }
            }

            return attachmentNames.ToArray();
        }

        /// <summary>
        /// The value of the document element, represented as text.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public static string GetItemValueText(NotesDocument document, string itemName)
        {
            #region dbg info
            //Array items = (document.Items as Array);

            //var field = new Dictionary<string, string>();

            //if (items != null)
            //{
            //    foreach (object iObj in items)
            //    {
            //        NotesItem item = iObj as NotesItem;
            //        if (item != null)
            //        {
            //            field.Add(item.Name, item.Text);
            //        }
            //    }
            //}
            #endregion

            Array vArray = document.GetItemValue(itemName) as Array;
            if (vArray != null)
            {
                System.Text.StringBuilder values = new System.Text.StringBuilder(vArray.Length);
                foreach (object vObj in vArray)
                {
                    string vText = vObj.ToString();
                    if (String.IsNullOrEmpty(vText))
                        continue;
                    if (values.Length > 0)
                        values.AppendFormat(";{0}", vText);
                    else
                        values.Append(vText);
                }
                return values.ToString();
            }
            return String.Empty;
        }
    }
}
